package com.zbkj.crmeb.plat.by.controller;

import com.alibaba.fastjson.JSON;
import com.common.CommonPage;
import com.common.PageParamRequest;
import com.constants.Constants;
import com.crmeb.core.common.CommonResult;
import com.crmeb.core.exception.CrmebException;
import com.crmeb.core.exception.ExceptionCodeEnum;
import com.utils.monitor.DingMsgEnum;
import com.utils.monitor.DingTalkMsgUtil;
import com.zbkj.crmeb.chant.request.TikTokProductQueryRequest;
import com.zbkj.crmeb.merchant.admin.controller.AdminBaseController;
import com.zbkj.crmeb.plat.by.dto.Result;
import com.zbkj.crmeb.plat.by.service.PlatProductService;
import com.zbkj.crmeb.plat.dto.PlatProductRequest;
import com.zbkj.crmeb.plat.enums.MerPlatEnums;
import com.zbkj.crmeb.task.plat.poizon.PoizonApi;
import com.zbkj.crmeb.task.plat.poizon.dto.PoizonResponseBodyDto;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author: wsl
 * @date: 2024/3/10 0010 21:25
 * @ClassName: PlatProductController
 */
@RestController("PlatProductController")
@RequestMapping("api/plat")
public class PlatProductController extends AdminBaseController {
    private static final Logger logger = LoggerFactory.getLogger(PlatProductController.class);

    @Autowired
    private PlatProductService platProductService;
    @Autowired
    private PoizonApi poizonApi;

    @ApiOperation(value = "推送平台")
    @RequestMapping(value = "/push/product", method = RequestMethod.POST)
    public CommonResult<List<String>> pushProduct(@RequestBody PlatProductRequest platProductRequest, HttpServletRequest servletRequest) {
        try {
            CommonResult<List<String>> commonResult = platProductService.pushProduct(platProductRequest, getMerId(servletRequest));
            if (commonResult.getCode() != ExceptionCodeEnum.SUCCESS.getCode()) {
                DingTalkMsgUtil.sendMsg(DingMsgEnum.PLAT, "推送商品失败：" + commonResult.getMessage());
            }
            return commonResult;
        } catch (CrmebException e) {
            return CommonResult.failed(JSON.toJSONString(Result.getSuccessResult(e.getMessage())));
        } catch (Exception e) {
            return CommonResult.failed(JSON.toJSONString(Result.getSuccessResult("接口处理异常")));
        }

    }

    @ApiOperation(value = "修改商品编码")
    @RequestMapping(value = "/push/product/update", method = RequestMethod.POST)
    public CommonResult<Object> updateProduct(@RequestBody PlatProductRequest platProductRequest, HttpServletRequest servletRequest) {
        try {
            CommonResult<Object> commonResult = platProductService.updateProduct(platProductRequest, getMerId(servletRequest));
            return commonResult;
        } catch (Exception e) {
            return CommonResult.failed(JSON.toJSONString(Result.getSuccessResult("接口处理异常")));
        }

    }

    @ApiOperation(value = "获取推送的平台")
    @RequestMapping(value = "/platOfPush", method = RequestMethod.GET)
    public CommonResult<Object> getPlatOfPush(HttpServletRequest servletRequest) {
        Integer merId = getMerId(servletRequest);
        if (null == merId) {
            return CommonResult.success("");
        }
        MerPlatEnums enumByMerEnum = MerPlatEnums.getEnumByMerId(merId);
        if (enumByMerEnum == null) {
            return CommonResult.success("");
        }
        return CommonResult.success((Object) enumByMerEnum.getCode());
    }

    @ApiOperation(value = "获取得物平台推送要的数据")
    @RequestMapping(value = "/poizon/pushInfo", method = RequestMethod.GET)
    public CommonResult<Object> getPoizonPushInfo(Integer productId, HttpServletRequest servletRequest) {
        try {
            return platProductService.getPoizonPushInfo(productId, getMerId(servletRequest));
        } catch (Exception e) {
            return CommonResult.failed(JSON.toJSONString(Result.getSuccessResult("接口处理异常")));
        }
    }

    @ApiOperation(value = "获取抖店平台推送货品类目选项")
    @RequestMapping(value = "/tiktok/getGoodsCategoryList", method = RequestMethod.GET)
    public CommonResult<Object> getTikTokGoodsCategoryList(Integer productId, @RequestParam(value = "categoryCid") Long cid, HttpServletRequest servletRequest) {
        try {
            return platProductService.getTikTokGoodsCategoryList(productId, getMerId(servletRequest), cid);
        } catch (Exception e) {
            return CommonResult.failed(JSON.toJSONString(Result.getSuccessResult("接口处理异常")));
        }
    }

    @ApiOperation(value = "获取抖店平台推送货品品牌")
    @RequestMapping(value = "/tiktok/getGoodsBrandList", method = RequestMethod.GET)
    public CommonResult<Object> getTikTokGoodsBrandList(Integer productId, Long categoryPid, Long categoryCid, HttpServletRequest servletRequest) {
        try {
            return platProductService.getTikTokGoodsBrandList(productId, categoryPid, categoryCid, getMerId(servletRequest));
        } catch (Exception e) {
            return CommonResult.failed(JSON.toJSONString(Result.getSuccessResult("接口处理异常")));
        }
    }

    @ApiOperation(value = "获取仓库列表")
    @RequestMapping(value = "/tiktok/getWarehouseList", method = RequestMethod.GET)
    public CommonResult<Object> getTikTokWarehouseList(Integer productId, HttpServletRequest servletRequest) {
        try {
            return platProductService.getTikTokWarehouseList(productId, getMerId(servletRequest));
        } catch (Exception e) {
            return CommonResult.failed(JSON.toJSONString(Result.getSuccessResult("接口处理异常")));
        }
    }


    @ApiOperation(value = "获取创建货物属性 & 商品屬性")
    @RequestMapping(value = "/tiktok/getCategoryProp", method = RequestMethod.GET)
    public CommonResult<Object> getTikTokCategoryProp(Integer productId, @RequestParam(value = "categoryCid") Long categoryCid, HttpServletRequest servletRequest) {
        try {
            return platProductService.getCategoryProp(productId, categoryCid, getMerId(servletRequest));
        } catch (Exception e) {
            return CommonResult.failed(JSON.toJSONString(Result.getSuccessResult("接口处理异常")));
        }
    }
}
